package com.keycloak.admin.services;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.keycloak.admin.model.params.AliSmsParams;
import com.keycloak.admin.utils.JsonUtils;

import java.util.Map;


public class ALiSmsService {
    //对应你阿里云账户的 accessKeyId
    private static final String accessKeyId = "LTAI4FjsXa4GC4qWabnmQ5GL";
    //对应你阿里云账户的 accessKeySecret
    private static final String accessKeySecret = "hzx8igYqBh4DxkvZ7u0XREdE9WwnPk";
    //对应签名名称
    private static final String signName="悦道用车";

    /**
     * 短信发送
     * @param telphone 发送的手机号
     */
    public static AliSmsParams SendSms(String telphone,String templateCode,Map m) {
        String templateParams = JSONObject.toJSONString(m);
        DefaultProfile profile = DefaultProfile.getProfile("default",
                accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        //阿里云对应发送短信的服务器地址
        request.setDomain("dysmsapi.aliyuncs.com");
        //对应的版本号
        request.setVersion("2017-05-25");

        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", telphone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParams);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            AliSmsParams o = (AliSmsParams)JsonUtils.fromJson(data, AliSmsParams.class);
            System.out.println(response.getData());
            return o;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String QuerySendDetails(String CurrentPage,String PageSize, String PhoneNumber, String SendDate) {
        DefaultProfile profile = DefaultProfile.getProfile("default",
                accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        //阿里云对应发送短信的服务器地址
        request.setDomain("dysmsapi.aliyuncs.com");
        //对应的版本号
        request.setVersion("2017-05-25");

        request.setAction("QuerySendDetails");
        request.putQueryParameter("CurrentPage", CurrentPage);
        request.putQueryParameter("PageSize", PageSize);
        request.putQueryParameter("PhoneNumber", PhoneNumber);
        request.putQueryParameter("SendDate", SendDate);
        CommonResponse response=null;
        try {
             response = client.getCommonResponse(request);
            //System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response.getData();
    }

}
